 //控制层 
app.controller('itemCatController' ,function($scope,$controller,itemCatService){

    //引入分页的配置对象属性
    $scope.reload = true;
    $scope.paginationConf = {
        currentPage: 1,
        totalItems: 100,
        itemsPerPage: 5,
        pagesLength: 15,
        perPageOptions: [5, 10, 15, 20, 30, 40, 50],
        onChange: function(){
            if (!$scope.reload){
                return;
            }
            $scope.findItemCatListByParentId($scope.Id);
            $scope.reload = false;
            setTimeout(function () {
                $scope.reload = true;
            },200)
        }
    };

    //定义用于存放选中项ID的数组
    $scope.selectIds = [];
    //定义选中复选框的操作
    $scope.selectOne = function (event,id) {
        if (event.target.checked) //当选中复选框就把对应的ID放入数组中
            $scope.selectIds.push(id);
        else {
            var index = $scope.selectIds.indexOf(id);//得到此ID在数组中的下标
            if (index != -1)
                $scope.selectIds.splice(index,1);//根据下标移除数组中的元素
        }
    };

    //全选、反选
    $scope.selectAll = function (event,list) {
        if (event.target.checked) {
            //遍历从后台获取的列表
            angular.forEach(list,function (item) {
                item.checked = true;//选中所有复选框
                $scope.selectIds.push(item.id)//把选中的品牌ID放入数组中
            })
        }else {
            angular.forEach(list,function (item){
                item.checked = false;//取消所有复选框
                $scope.selectIds = [];//清空数组
            })
        }
    };

    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		itemCatService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(){
		itemCatService.findPage($scope.paginationConf.currentPage,$scope.paginationConf.itemsPerPage).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
	
	//查询实体 
	$scope.findOne=function(id){				
		itemCatService.findOne(id).success(
			function(response){
				$scope.entity= response;					
			}
		);				
	}
	
	//保存 
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.id!=null){//如果有ID
			serviceObject=itemCatService.update( $scope.entity ); //修改  
		}else{
			$scope.entity.parentId = $scope.itemCatId;//设置上级分类ID
			serviceObject=itemCatService.add( $scope.entity  );//增加
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询 
		        	$scope.findItemCatListByParentId($scope.itemCatId);//重新加载
				}else{
					alert(response.message);
				}
			}		
		);				
	}
	
	 
	//批量删除 
	$scope.dele=function(){			
		//获取选中的复选框			
		itemCatService.dele( $scope.selectIds ).success(
			function(response){
				if(response.success){
                    $scope.findItemCatListByParentId($scope.itemCatId);//重新加载
					$scope.selectIds=[];
				}						
			}		
		);				
	}

    // 定义分类级别
    $scope.grade = 1;
	//定义修改分类级别的方法
	$scope.setGrade = function(grade){
		$scope.grade = grade;
	}
    $scope.itemCatId = 0;
    //根据parentId查询itemCat列表
	$scope.findItemCatListByParentId = function(parentId){
		if (parentId)
			$scope.itemCatId = parentId;//定义全局变量itemCatId，用于记住上级分类ID

		itemCatService.findItemCatListByParentId($scope.itemCatId,
			$scope.paginationConf.currentPage,$scope.paginationConf.itemsPerPage).success(function (response) {
            $scope.list=response.rows;
            $scope.paginationConf.totalItems=response.total;//更新总记录数
            }
        );
	}
    //点击查询下级分类列表
	$scope.selectList = function (p_entity) {
		switch ($scope.grade){
			case 1://设置一级分类的内容
				$scope.entity1 = p_entity;
				$scope.entity2 = null;
                $scope.entity3 = null;
                break;
			case 2://设置二级分类的内容
                $scope.entity2 = p_entity;
                $scope.entity3 = null;
				break;
			case 3://设置三级分类的内容
                $scope.entity3 = p_entity;
				break
		}
        $scope.itemCatId = p_entity.id;//定义全局变量parentId，用于记住上级分类ID
        //根据parentId查询itemCat列表
        $scope.findItemCatListByParentId($scope.itemCatId,
			$scope.paginationConf.currentPage,$scope.paginationConf.itemsPerPage);
		//修改分类级别
        $scope.setGrade($scope.grade+1);
    }


});	
